<template>
    <div class="container home-page">
        <!-- 表单搜索和新增 -->
        <div class="home-head">
            <span class="home-title">项目名称:</span>
            <el-cascader
                @change="fnGetData(1)"
                :options="options"
                clearable
                filterable
                v-model="value"
                size="small"
                :props="{
                    label: 'title',
                    value: 'title',
                    children: 'children'
                }"
            />
        </div>
        <!-- /表单搜索和新增 -->

        <!-- 表单列表 -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            @sort-change="handleSortChanged"
        >
            <el-table-column
                label="序号"
                width="100px"
                type="index"
                align="center"
            />
            <el-table-column
                :formatter="emptyFormatter"
                prop="title"
                label="项目名称"
                show-overflow-tooltip
            />
            <el-table-column
                prop="sort"
                label="排序号"
                sortable='custom'
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="read_count"
                label="浏览量"
                show-overflow-tooltip
                sortable='custom'
            />
            <el-table-column
                prop="show_status"
                label="项目状态"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.show_status == 0">不显示</span>
                    <span v-if="scope.row.show_status == 1">正常显示</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="发布时间"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
            >
                <template slot-scope="scope">
                    <span
                        class="curse-delete"
                        @click="fnDeleteData(scope.row)"
                    >从首页移除</span>
                </template>
            </el-table-column>
        </el-table>
        <!-- /表单列表 -->

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="fnGetData"
            />
        </div>
        <!-- /分页 -->
    </div>
</template>

<script>
const sortFieldMap = {
    sort: '1',
    read_count: '2'

};
const sortTypeMap = {
    'ascending': 0,
    'descending': 1
};

export default {
    data () {
        return{
            value: '',
            options: [],
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            sortData: {
                field: 0,
                by: ''
            },
            formAdd: {
                type: '',
                recommendNumber: ''
            },
            rules: {
                type: [
                    { required: true, message: '请选择工作类型', trigger: 'change' }
                ],
                recommendNumber: [
                    { required: true, message: '请输入对应项编号', trigger: 'blur' },
                    { min: 1, max: 40, message: '编号长度不超过40个字符', trigger: 'blur' }
                ]
            },
        };
    },
    created () {
        this.$store.commit('SET_HEADER', [
            {
                title: "校园代理",
            },
            {
                title: '工作'
            },
            {
                title: "首页推荐"
            }]
        );
        this.getSimpleList();
        this.fnGetData(1);
    },
    methods: {
        /**
         * 获取简要工作推荐列表
         */
        getSimpleList () {
            this.$post('/student/work_recommend%5Cget_simple_list', {
            },resp => {
                if (resp.code == 1) {
                    this.options = resp.data.list;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'info',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         *  获取表格数据
         */
        fnGetData (page) {
            this.$post('/student/work_recommend%5Cget_list', {
                page_id: page || this.page.now_page,
                keyword: this.value[0] == '' ? '' : this.value[0],
                sort_field: this.sortData.field,
                sort_type: this.sortData.by
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         *  修改排序
         */
        fnEditSort (row) {
            this.$prompt('修改排序', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(0|\+?[1-9][0-9]*)$/,
                inputPlaceholder: '请输入序号',
                inputErrorMessage: '请输入正整数',
                closeOnClickModal: false,
            }).then(({ value }) => {
                this.$post('/student/work_recommend%5Cset_sort', {
                    id: row.id,
                    sort: value
                }, resp => {
                    if (resp.code == 1) {
                        this.$set(row, 'sort', value);
                        this.$message({
                            type: 'success',
                            message: '操作成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'info',
                            message: '操作失败',
                            center: true
                        });
                    }
                });
            }).catch(() => {
            });
        },

        /**
         * 移除
         */
        fnDeleteData (no) {
            this.$confirm('确认从推荐中移除该项目？', '提示', {
                confirmButtonText: '删除',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/work_recommend%5Cdelete', {
                    id: no.id
                }, resp => {
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                        this.fnGetData();
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'error',
                            message: '删除失败',
                            center: true
                        });
                    }
                });
            }).catch(() => {
            });
        },

        /**
         *  排序
         */
        handleSortChanged (data) {
            if (data.order) {
                this.sortData.field = sortFieldMap[data.prop];
                this.sortData.by = sortTypeMap[data.order];
            } else {
                this.sortData.field = 0;
                this.sortData.by = 1;
            }

            this.fnGetData();
        },

        /**
         * 列表渲染辅助函数：列表项为空处理
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        .home-head{
            margin-bottom: 20px;
            position: relative;
            display: flex;
            align-items: center;
            .home-title{
                color: #606266;
                margin-right: 10px;
            }
            .el-input {
                width: 200px;
            }
        }
        .table-wrapper {
            flex: 1;
            background-color: #fff;
            .sort-wrap {
                display: flex;
                align-items: center;
                .sort-spliter {
                    width: 1px;
                    height: 12px;
                    background: rgba(235, 235, 235, 1);
                    margin: 0 10px;
                }
                .btn-sort {
                    font-size: 14px;
                    line-height: 1;
                    color: #ccc;
                    &:hover {
                        color: #20a0ff;
                    }
                }
            }
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .sort-wrap {
            display: flex;
            align-items: center;
            cursor: pointer;
            .sort-spliter {
                width: 1px;
                height: 12px;
                background: rgba(235, 235, 235, 1);
                margin: 0 10px;
                }
                .btn-sort {
                font-size: 14px;
                line-height: 1;
                color: #ccc;
                &:hover {
                    color: #20a0ff;
                }
            }
        }
        .curse-delete{
            color: #20a0ff;
            cursor: pointer;
            margin-right: 10px;
        }
        .el-form-item__content {
            display: flex;
        }
        .sort-model {
            margin-right: 10px;
        }
        .work-name {
            margin-right: 15px;
        }
        .input-width {
            width: 60%;
        }
        .rel-button {
            margin-left: 280px;
            margin-right: 10px;
        }
    }
</style>
<style lang="less">
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
</style>